package com.itheima.app.mq;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 工具类的目的: 发送mq消息
 */
@Component
public class MovementMQManager {
    //针对动态的操作
    public static final Integer MOVEMENT_PUBLISH = 1;// 发动态
    public static final Integer MOVEMENT_BROWSE = 2;// 浏览动态
    public static final Integer MOVEMENT_LIKE = 3;// 点赞
    public static final Integer MOVEMENT_LOVE = 4;// 喜欢
    public static final Integer MOVEMENT_COMMENT = 5;// 评论
    public static final Integer MOVEMENT_DISLIKE = 6;// 取消点赞
    public static final Integer MOVEMENT_DISLOVE = 7;// 取消喜欢

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Reference
    private MovementService movementService;
    //构建一个方法 发送mq相关消息
    // 谁 对 什么内容  进行了  什么操作
    public void sendMsg(String movementId , Integer type){
        //谁(userId)    什么内容(movementId) 查询动态   什么操作(type)
        Long userId = UserHolder.getId();
        Map map = new HashMap<>();
        map.put("userId" , userId); //谁
        Movement movement = movementService.findMovementByMovementId(movementId);
        map.put("pid" , movement.getPid());//什么内容
        map.put("type" , type);//做了什么操作
        rocketMQTemplate.convertAndSend("tanhua-recommend-161" ,map);
    }
}
